using System;
using System.IO;
using System.Collections.Generic;

namespace TicketSellerService {
	public class FlightTicketService : IFlightTicketService {
	
		public AvailableFlights search( string source, string destination, string date) {
			List<Flight> availFlights = new List<Flight>( );
			foreach( Flight flight in TicketsDB.getInstance( ).getFlights( )) {
				if ( flight.source.Equals( source) && flight.destination.Equals( destination) &&
				     flight.date.Equals( Convert.ToDateTime( date))) {
					availFlights.Add( flight);
				}
			}
			return new AvailableFlights( availFlights);
		}
		
		public TicketReservation reserve( int flightNumber, string date) {
			TicketReservation reservation = null;
			TicketsDB.getInstance( ).makeReservation( flightNumber, Convert.ToDateTime( date), out reservation);
			return reservation;
		}
		
		public string cancel( int rsrvId) {
			TicketsDB.Status status = TicketsDB.getInstance( ).cancelReservation( rsrvId);
			if ( status == TicketsDB.Status.CANCELATION_FAILED ) {
				return "Failed, no such reservation";
			} 
			return "OK";
		}
		
	}
}